﻿<%@ Page Title="" Language="C#" MasterPageFile="~/UI/Main.Master" AutoEventWireup="true" CodeBehind="kiemke.aspx.cs" Inherits="QLBH.UI.kiemke" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <style type="text/css">
        .popover {
            width: 276px;
        }

        .popover-title {
            padding: 0px;
            text-align:center;
            padding-bottom:8px;
        }


        .popover-content {
            max-height: 276px;
            overflow: auto;
        }

        table th {
            font-size: 16px;
        }

        #table_quyen th {
            font-size: inherit;
        }

        table tr td {
            text-align: left;
        }
    </style>


    <div class="row">
        <div class="col-md-5">
        </div>
        <div class="col-md-3 text-right">
        </div>
        <div class="col-md-4 text-right" style="padding-bottom: 5px;">
            <span id="btn_refresh" data-toggle="popover" class="btn btn-success"><span class="glyphicon glyphicon-search"></span></span>
            <span id="btn_info" data-toggle="popover" class="btn btn-primary"><span class="glyphicon glyphicon-info-sign"></span></span>

        </div>

    </div>
    <div class="row">
        <div class="col-md-12 text-center" id="lazyScroll" style="overflow: auto; max-height: 310px;">

            <table id="table_ds_tonkho" class="table table-bordered " style="min-width: 900px;">
                <thead>
                    <tr>
                        <th class="text-center" style="width: 30px;">
                            <input id="chk_check_all" type="checkbox" value="-1" />
                        </th>
                        <th class="text-center" style="width: 50px;">STT</th>
                        <th style="width: 100px;">Ngày</th>
                        <th style="width: 120px;">Kho</th>
                        <th>Tên sản phẩm </th>
                        <th style="width: 100px;">Tồn đầu</th>
                        <th style="width: 100px;">Nhập</th>
                        <th style="width: 100px;">Xuất</th>
                        <th style="width: 100px;">Tồn cuối</th>
                        <th style="width: 120px;">Đơn giá BQ</th>
                    </tr>
                </thead>
                <tbody>
                </tbody>
            </table>
        </div>
    </div>

    <div id="popoversearch" style="display: none">
        <div class="form-group">
            <input type="date" class="form-control" id="txt_tu_ngay" value="<%=DateTime.Now.ToString("yyyy-MM-dd") %>" />
        </div>
        <div class="form-group">
            <input type="date" class="form-control" id="txt_den_ngay" value="<%=DateTime.Now.ToString("yyyy-MM-dd") %>" />
        </div>
        <div class="form-group">
            <input type="search" class="form-control" id="txt_search" placeholder="Nhập vào tên sản phẩm" />
        </div>

        <div class="form-group">
            <select class="form-control" id="cbo_kho_hang">
            </select>
        </div>
        <span id="btn_hoan_tat" class="btn btn-primary pull-right">&nbsp Tìm &nbsp</span>

    </div>

    <div id="popoverds" style="display: none">
        <table id="table_ds" class="table table-bordered">
            <tbody>
            </tbody>
        </table>

    </div>

    <script src="slidebars/scripts/bootstrap/js/jquery.min.js"></script>
    <script src="slidebars/scripts/bootstrap/js/bootstrap.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {

            function BlockUIPopover() {
                $('.popover').block({
                    message: "<img src='images/ajax-loader.gif'/>", css: { left: '50%', cursor: 'wait', backgroundColor: 'none', border: 'none', width: '50px' },
                    overlayCSS: { backgroundColor: 'none', opacity: 0.0, cursor: 'wait' }
                });
            }

            $("#btn_refresh").click(function (event) {
                $(this).popover('show');
            });


            $('body').on('click', 'input#rdpn', function (event) {
                $("#table_ds tbody").html('');
                BlockUIPopover();
                TimPhieuNhap('', 0);
            });
            $('body').on('click', 'input#rdpx', function (event) {
                $("#table_ds tbody").html('');
                BlockUIPopover();
                TimPhieuXuat('', 0);
            });

            $("#btn_info").click(function (event) {
                var k = $('#table_ds_tonkho tbody tr.info').length;
                if (k != 0) {
                    $(this).popover('show');
                    BlockUIPopover();
                    if ($("#rdpn").attr("checked", "checked")) {
                        $("#table_ds tbody").html('');
                        TimPhieuNhap('', 0);
                    }
                }
            });


            $("#btn_info").popover({
                html: true,
                content: function () {
                    return $('#popoverds').html();
                },
                title: function () {
                    return ' <h4 class="text-center">Chứng từ</h4> <label class="radio-inline">' +
             '<input type="radio" name="inlineRadioOptions" id="rdpn" checked="checked" value="option1"/>' +
             'Phiếu Nhập' +
             '</label>' +
             '<label class="radio-inline">' +
             '<input type="radio" name="inlineRadioOptions" id="rdpx" value="option2"/>' +
             'Phiếu Xuất' +
             '</label>';
                },
                placement: 'bottom',
                trigger:'Click'
            });

            $("#btn_refresh").popover({
                html: true,
                content: function () {
                    return $('#popoversearch').html();
                },
                title: function () {
                    return ' <h4 class="text-center">Tìm kiếm</h4>';
                },
                placement: 'bottom',
                trigger: 'Click'
            });

            $('body').on('click', 'table#table_ds tbody tr', function (e) {

                $("#table_ds_pn tbody tr").removeClass('info');
                $(this).addClass("info");
                //alert($(this).parent().parent().parent().find('td:not(:empty):first').html());
                if ($("#rdpn").is(':checked') ==true) {
                    window.open("baocao.aspx?ma_phieu_nhap=" + $(this).find('td:not(:empty):first').html(), '_blank');
                }
                else
                window.open("baocao.aspx?ma_phieu_xuat=" + $(this).find('td:not(:empty):first').html(), '_blank');

            });


            $('#btn_info').on('shown.bs.popover', function () {
                var h = $(window).height() - 239;
                $(".popover-content").css({ 'max-height': h });
            });

            $('#btn_refresh').on('shown.bs.popover', function () {
                var h = $(window).height() - 239;
                $(".popover-content").css({ 'max-height': h });
            });

            function TimPhieuNhap(ma_phieu_nhap, tong_so_dong) {

                var DTO = {
                    'ma_phieu_nhap': ma_phieu_nhap,
                    'tong_so_dong': tong_so_dong
                };
                $.ajax({
                    type: "POST",
                    url: "../BUSINESS/Services.asmx/TimPhieuNhapTheoMa",
                    data: JSON.stringify(DTO),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                       // alert(data.d);
                        $(".popover").unblock();
                        $.each($.parseJSON(data.d), function (idx, obj) {
                            tong_so_dong += 1;
                            $("#table_ds tbody").append(
                            '<tr>' +
                            '<td class ="hidden">' + obj.MA_PHIEU_NHAP + '</td>' +
                            '<td>Mã:&nbsp<b>' + obj.MA_PHIEU_NHAP + '</b><br/>Kho:&nbsp' + obj.KHO_HANG.TEN_KHO_HANG + '<br/>Ngày:&nbsp' + getFDate(obj.NGAY_NHAP) + '</br>' +

                            '</td>' +
                            ' <td class ="hidden">' + obj.KHO_HANG.MA_KHO_HANG + '</td>' +
                            ' </tr>');
                        });

                    },
                    error: function (data) {
                        $(".popover").unblock();
                        console.log(data.d);
                    }

                });
            }

            function TimPhieuXuat(ma_phieu_xuat, tong_so_dong) {

                var DTO = {
                    'ma_phieu_xuat': ma_phieu_xuat,
                    'tong_so_dong': tong_so_dong
                };
                $.ajax({
                    type: "POST",
                    url: "../BUSINESS/Services.asmx/TimPhieuXuatTheoMa",
                    data: JSON.stringify(DTO),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                       // alert(data.d);
                        $(".popover").unblock();
                        $.each($.parseJSON(data.d), function (idx, obj) {
                            tong_so_dong += 1;
                            $("#table_ds tbody").append(
                            '<tr>' +
                            ' <td class ="hidden">' + obj.MA_PHIEU_XUAT + '</td>' +
                            ' <td>Mã:&nbsp<b>' + obj.MA_PHIEU_XUAT + '</b><br/>Kho:&nbsp' + obj.KHO_HANG.TEN_KHO_HANG + '<br/>Ngày:&nbsp' + getFDate(obj.NGAY_XUAT)+'</td>' +
                            ' <td class ="hidden">' + obj.KHO_HANG.MA_KHO_HANG + '</td>' +
                            ' </tr>');
                        });

                    },
                    error: function (data) {
                        $(".popover").unblock();
                        console.log(data.d);
                    }

                });
            }

            $('body').on('click', 'span#btn_hoan_tat', function () {
                BlockUI();
                LayDanhSachTonKho($("#txt_tu_ngay").val(), $("#txt_den_ngay").val(), $("#txt_search").val(), $("#cbo_kho_hang").val());
            });

            function BlockUI() {
                $.blockUI({
                    message: "<img src='images/ajax-loader.gif'/>", css: { left: '50%', cursor: 'wait', backgroundColor: 'none', border: 'none', width: '50px' },
                    overlayCSS: { backgroundColor: 'none', opacity: 0.0, cursor: 'wait' }
                });
            }


            $("#table_ds_tonkho").on("click", ":checkbox", function () {
                if ($(this).val() == -1) {
                    if ($(this).is(':checked')) {
                        $("#table_ds_tonkho tr").addClass("info");
                        $("#table_ds_tonkho tr:first").removeClass("info");
                    }
                    else
                        $("#table_ds_tonkho tr").removeClass("info");
                    return;
                }

                if ($(this).is(':checked')) {
                    $(this).parents("tr:first").addClass("info");
                }
                else {
                    $(this).parents("tr:first").removeClass("info");
                }
            });


            function BlockUIPopover() {
                $('.popover').block({
                    message: "<img src='images/ajax-loader.gif'/>", css: { left: '50%', cursor: 'wait', backgroundColor: 'none', border: 'none', width: '50px' },
                    overlayCSS: { backgroundColor: 'none', opacity: 0.0, cursor: 'wait' }
                });
            }

            $('#btn_refresh').on('shown.bs.popover', function () {
                BlockUIPopover();
                TimKhoHang('', 0);
                var date = new Date().toDateString();

            });

            function TimKhoHang(ten_kho_hang, tong_so_dong) {

                var DTO = {
                    'ten_kho_hang': ten_kho_hang
                };
                $.ajax({
                    type: "POST",
                    url: "../BUSINESS/Services.asmx/TimKhoHangTheoTen",
                    data: JSON.stringify(DTO),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        $("#cbo_kho_hang").html('');
                        $.each($.parseJSON(data.d), function (idx, obj) {
                            $("#cbo_kho_hang").append(
                                   '<option value="' + obj.MA_KHO_HANG + '">' + obj.TEN_KHO_HANG + '</option>');
                        });
                        $(".popover").unblock();
                    },
                    error: function (data) {
                        $(".popover").unblock();
                        console.log(data.d);
                    }

                });
            }

            onload = function () {
                // BlockUI();
                //var tables = document.getElementById("table_kho");
                // LayDanhSachTonKho(new Date, new Date, '');
                //  TimKhoHang('', 0);
            }

            function getFDate(dateObject) {

                var today = new Date(dateObject);
                if (dateObject == '') {
                    today = new Date();
                }
                var dd = today.getDate();
                var mm = today.getMonth() + 1; //January is 0!

                var yyyy = today.getFullYear();
                if (dd < 10) {
                    dd = '0' + dd
                }
                if (mm < 10) {
                    mm = '0' + mm
                }
                var today = dd + '/' + mm + '/' + yyyy;
                return today;
            }

            function LayDanhSachTonKho(tu_ngay, den_ngay, ten_san_pham, ma_kho_hang) {

                var DTO = {
                    'tu_ngay': tu_ngay,
                    'den_ngay': den_ngay,
                    'ten_san_pham': ten_san_pham,
                    'ma_kho_hang': ma_kho_hang
                };
                $.ajax({
                    type: "POST",
                    url: "../BUSINESS/Services.asmx/LayDanhSachTonKho",
                    data: JSON.stringify(DTO),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {

                        var tong_so_dong = 0;
                        $("#table_ds_tonkho tbody").html('');
                        $.each($.parseJSON(data.d), function (idx, obj) {
                            tong_so_dong += 1;
                            $("#table_ds_tonkho tbody").append(
                            '<tr>' +
                            ' <td>' +
                            ' <input type="checkbox" value="' + tong_so_dong + '" />' +
                            ' </td>' +
                            ' <td>' + tong_so_dong + '</td>' +
                            ' <td>' + getFDate(obj.NGAY_TON_KHO) + '</td>' +
                            ' <td>' + obj.KHO_HANG.TEN_KHO_HANG + '</td>' +
                            ' <td>' + obj.SAN_PHAM.TEN_SAN_PHAM + '</td>' +
                            ' <td class="text-right">' + formatCurrency(obj.TON_DAU_KY) + '</td>' +
                            ' <td class="text-right">' + formatCurrency(obj.NHAP_TRONG_KY) + '</td>' +
                            ' <td class="text-right">' + formatCurrency(obj.XUAT_TRONG_KY) + '</td>' +
                            ' <td class="text-right">' + formatCurrency(obj.TON_CUOI_KY) + '</td>' +
                            ' <td class="text-right">' + formatCurrency(obj.DON_GIA_BINH_QUAN) + '</td>' +
                            ' </tr>');
                        });

                    },
                    error: function (data) {
                        console.log(data.d);
                    }

                });
            }
        });
    </script>
</asp:Content>
